<?php
/**
 * Admin 首页模块
 * Created by PhpStorm.
 * User: 65736
 * Date: 2018/12/12 0012
 * Time: 18:49
 */

namespace app\admin\controller;

use app\common\library\CommonFun;
use app\common\model\rbac\RbacAdminModel;
use app\common\model\rbac\RbacNodeModel;
use app\common\widget\WidgetEditForm;
use app\common\widget\WidgetSearchForm;
use app\common\widget\WidgetTableContainer;
use think\Config;
use think\db\Query;
use think\Exception;

class Index extends AdminCommon
{
    /**
     * 初始化方法
     * @throws Exception
     */
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页方法
     * @throws Exception
     */
    public function index() {
        // 分配左侧功能列表
        $this->admin_id= 1;
        $this->admin_role_id = 2;

        // 头部menu
        $header_menu = RbacNodeModel::NodeGroupAction;
        $this->assign('header_menu', $header_menu);

        // 左侧菜单
        $left_menu = $this->rbac_model->getLeftMenuList( $this->admin_id,  $this->admin_role_id );
        $this->assign('left_menu', CommonFun::json_encode2($left_menu));

        return $this->fetch();
    }

    /**
     * @return mixed
     * @throws Exception
     * @throws \Exception
     */
    public function testEdit() {
        $m = RbacAdminModel::getInstance();
        //dump($m->getOptions());
        //$res = $m->getTableFieldsComment();
        //$res = $m->where("id=1")->value('id');
        //$res = RbacAdminModel::all([1,2,3,4,5,6,7]);

        $map = array(
            //'id' => [['in', [1,3]], ['eq', 2], 'or'],
//            'id' => [array(['in', [1,3]], ['eq', 2]), 'or'],
            'id' => 2,
            'status' => 1,
        );
        $map1 = array(
            'status' => 1,
        );
        $map2 = array(
            'nick_name' => '小唐',
            'status' => -1,
            'sfsf' => 'efsdfs',
        );
        //dump($res);

        //$res = Db::query("SHOW FULL COLUMNS FROM rbac_node");
//        $res = $m->where(function ($query) use ($map) {
//            if ( $query instanceof Query)
//            $query->where($map);
//        })->whereOr(function ($query) use ($map2) {
//            $query->where($map2);
//        });
//        $where = array(
//            "id&status" => ['eq', [1,-1]]
//        );
//        $m->where($where);
        $m->where($map2);

        dump($m->muti_get_data_by_where());

        $form = new WidgetEditForm();
        $form->addTextInput()->setId("a")->setName('b')->setValue('sds')->setPlaceholder('请填写aaa')->setTitle("文章名称");
        $column = $form->addFormColumn();
        $column->addTextInput()->setId("l")->setName('l')->setValue('xxx')->setPlaceholder('请填写xxx')->setTitle("文章标题");
        $column->addTextInput()->setId("l")->setName('l')->setValue('xxx')->setPlaceholder('请填写xxx')->setTitle("文章标题");
        $column->addCheckBoxInput()->setCheckBoxList(['1'=>'你', '2'=>'我', '3'=>'他'])->setValue('1')->setTitle("文章种类")->setName("aaa");
        $column->addRadioInput()->setRadioList(['1'=>'sdfs', '2'=>'sdfsf'])->setValue('2')->setTitle("是否推送")->setName("bbb");
        $column->addTextAreaInput()->setId("l")->setName('l')->setValue('xxx')->setPlaceholder('请填写xxx')->setTitle("文章标题");
        $column->addCKeditorInput()->setId("ck")->setName("ck")->setValue("fsdfsfsdf")->setPlaceholder("请填写yyy")->setTitle("文章详情");

        $this->assign('form', $form->render());
        return $this->fetch(__FUNCTION__);
    }

    /**
     * 后台欢迎页
     * @throws Exception
     * @throws \Exception
     */
    public function welcome(){
        // breadcrumb
        $this->assign('breadcrumb', 'sdfsdf');

        // search form
        $searchForm = new WidgetSearchForm();
        $searchForm->addTextInput()->setId('text1')->setPlaceholder('关键字');
        $searchForm->addSelectInput()->setOptions( array( 1=>'a', 2=>'b' ) )->setId('select1')->setValue([1,2])->setPlaceholder('币种')->setMuti()->setSearch();
        $this->assign('search_form', $searchForm->render());

        // Table
        $node = RbacNodeModel::getInstance();
        $id_list = $node->search_id_list([], $this->page);
        $data_list = $node->muti_get_data_by_id_list($id_list);
        $fieldTitle = [
            "id" => "编号",
            "group" => "分组",
            "name" => "名称",
            "title" => "显示",
            "status" => "状态",
            "remark" => "备注",
            "sort" => "排序",
            "pid" => "父级",
        ];

        $container = new WidgetTableContainer();
        $table = $container->addDataListTable()->setWidthColMd(6)->setWidthColSm(6)->setWidthColXs(6)->setWidthColLg(6);
        $table->setColumnFieldTitle( $fieldTitle )->setPageList($node->page_list);
        $table->setDataList( $data_list );
        $table->addColButton('group')->setButtonSizeXs()->setButtonStylePrimary()->setAction("index", "testEdit")->setClass('editData');

        $table->addColToolTip('name')->setTitle('编辑')->addIcon()->setColorDanger()->setfaEditIcon();

        $table->addColToolTip('name')->setTitle('删除')
            ->setAction("index", "testEdit")->setClass('editData')
            ->addIcon()->setColorDefault()->setfaCloseIcon();

        $table->addColumn("edit", "操作");
        $this->assign( 'table', $container->render() );

        return $this->fetch();
    }


}